<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <!-- 搜索模块 -->

      <el-form ref="searchMap" :inline="true" :model="searchMap" class="serach_wrapper flex-space-between">
        <div class="search">
          <el-form-item prop="noticeTitle" size="small">
            <el-input v-model="searchMap.noticeTitle" placeholder="公告标题"></el-input>
          </el-form-item>
          <el-form-item prop="createBy" size="small">
            <el-input v-model="searchMap.createBy" placeholder="操作人员"></el-input>
          </el-form-item>
          <el-form-item prop="noticeType" style="width: 220px" size="small">
            <el-select v-model="searchMap.noticeType" placeholder="公告类型" clearable>
              <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item class="flex">
            <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
        <div class="edit">
          <el-form-item class="flex">
            <el-button @click="handleAdd()" class="btn_primary" icon="el-icon-plus">
              新增
            </el-button>
          </el-form-item>
        </div>
      </el-form>
      <!-- 搜索模块over -->

      <!-- 表格模块 -->
      <div class="table_container">
        <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
          <el-table-column label="序号" align="center" prop="noticeId" width="100" />
          <el-table-column label="公告标题" align="center" prop="noticeTitle" :show-overflow-tooltip="true" />
          <el-table-column label="公告类型" align="center" prop="noticeType" width="100">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_notice_type" :value="scope.row.noticeType" />
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status" width="100">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_notice_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建者" align="center" prop="createBy" width="120" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="220">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="220">
            <template slot-scope="scope">
              <div class="button-group">
                <span type="text" class="tag_primary" size="small" @click="handleEdit(scope.row)">编辑</span>
                <span type="text" class="tag_warning" size="small" @click="handleDelete(scope.row)">删除</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 添加或修改通知对话框 -->
      <el-dialog :title="title" :visible.sync="dialogVisibleNotice" :close-on-click-modal="false" append-to-body>
        <el-form ref="form" :model="formModel" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="公告标题" prop="noticeTitle" size="small">
                <el-input v-model="formModel.noticeTitle" placeholder="请输入公告标题" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公告类型" prop="noticeType" size="small">
                <el-select v-model="formModel.noticeType" placeholder="请选择公告类型">
                  <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="状态">
                <el-radio-group v-model="formModel.status">
                  <el-radio v-for="dict in dict.type.sys_notice_status" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内容">
                <editor v-model="formModel.noticeContent" :min-height="192" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleNotice = false" class="btn_cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm" class="btn_primary">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api";
export default {
  name: "notice",
  dicts: ['sys_notice_status', 'sys_notice_type'],
  data () {
    return {
      // 加载状态
      loading: false,
      //搜索条件
      searchMap: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      //搜索条件
      noticeList: [],
      // 总条数
      total: 0,
      // 弹框状态 
      dialogVisibleNotice: false,
      // 弹框标题
      title: '',
      // 表单数据
      formModel: {},
      // 表单校验
      rules: {
        noticeTitle: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        noticeType: [
          { required: true, message: "公告类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  computed: {
    pageTitle () {
      return '公告管理'
    }
  },
  created () {
    this.getList()
  },
  mounted () {

  },
  watch: {},
  methods: {
    getList () {
      this.loading = true;
      listNotice(this.searchMap).then(res => {
        this.noticeList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.formModel = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
    },
    /** 搜索按钮操作 */
    handleSearch () {
      this.formModel.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset () {
      this.$nextTick(() => {
        this.$refs.searchMap.resetFields();
        this.handleSearch();
      })
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.dialogVisibleNotice = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleEdit (row) {
      this.reset();
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(res => {
        this.formModel = res.data;
        this.dialogVisibleNotice = true;
        this.title = "修改公告";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formModel.noticeId != undefined) {
            updateNotice(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message.success("修改成功");
                this.dialogVisibleNotice = false;
                this.getList();
              }
            });
          } else {
            addNotice(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message.success("新增成功");
                this.dialogVisibleNotice = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const noticeIds = row.noticeId || this.ids
      this.$confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function () {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      })
    }
  },
}
</script>

<style lang='less' scoped>
</style>